from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from .models import UserMessage


def index(request):
    # messages = UserMessage.objects.filter(name='补中松')
    # for message in messages:
    #     print(message.name)
    return render(request, 'message_form.html',{
        'my_message':""
    })


def message(request):
    if request.method == 'POST':
        user_message = UserMessage()
        user_message.name = request.POST.get('name', '')
        user_message.email = request.POST.get('email', '')
        user_message.address = request.POST.get('address', '')
        user_message.message = request.POST.get('message', '')
        user_message.save()
    return HttpResponseRedirect(reverse('form:index'))
